package com.glory.wxelectricitypda.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.ScrapListAdapter;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.bean.ResponseQueryBean;
import com.glory.wxelectricitypda.bean.ScrapBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ScrapListActivity extends FreeUI_BaseActivity {


    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.etNum)
    FreeUI_ClearEditText etNum;
    @BindView(R.id.llScrap)
    LinearLayout llScrap;
    @BindView(R.id.rvInfoList)
    RecyclerView rvInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private LotBean lotBean;
    private ScrapListAdapter scrapListAdapter;
    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_scrap_list;
    }

    @Override
    protected void initView() {
        lotBean = (LotBean) getIntent().getSerializableExtra("lotBean");
        tvLot.setText(lotBean.getLOTID());

        rvInfoList.setLayoutManager(new LinearLayoutManager(ScrapListActivity.this));
        rvInfoList.setItemAnimator(new DefaultItemAnimator());
        rvInfoList.addItemDecoration(new DividerItemDecoration(ScrapListActivity.this, 1));
        scrapListAdapter = new ScrapListAdapter(null);
        scrapListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        scrapListAdapter.isFirstOnly(true);
        rvInfoList.setAdapter(scrapListAdapter);


        freeUI_titleView.setTitle("报废填写");
        freeUI_titleView.setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(getApplicationContext(), "是", "否", "是否要保存已填写内容", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        verifyInput();
                        freeUI_generalFragmentDialog.dismiss();
                    }
                }, new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        freeUI_generalFragmentDialog.dismiss();
                        Intent in = new Intent();
                        in.putExtra("resultLotBean", lotBean);
                        setResult(2102, in);
                        finish();
                    }
                });
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "freeUI_generalFragmentDialog");
            }

        });
        freeUI_titleView.setRightListener("保存", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                verifyInput();
            }
        });

    }

    private void verifyInput() {
        String scrapNum = etNum.getText().toString().trim();
        if (scrapNum.length() == 0) {
            showToast("请输入报废数量", false);
            return;
        }
        if (Integer.parseInt(scrapNum) == 0) {
            showToast("报废数量不能为0", false);
            return;
        }
        int listNum = 0;
        List<ScrapBean> qtyList = new ArrayList<>();
        for (ScrapBean scrapBean : scrapListAdapter.getData()) {
            if (scrapBean.getQTY().length() > 0) {
                qtyList.add(scrapBean);
                listNum += Integer.parseInt(scrapBean.getQTY());
            }
        }
        if (Integer.parseInt(scrapNum) != listNum) {
            showToast("报废数量必须等于报废列表总和", false);
            return;
        }
        if (Integer.parseInt(scrapNum) != listNum) {
            showToast("报废数量必须等于报废列表总和", false);
            return;
        }
        Intent in = new Intent();
        in.putExtra("resultLotBean", new LotBean(lotBean.getLOTID(), scrapNum, qtyList));
        setResult(2101, in);
        finish();
    }

    @Override
    protected void initData() {
        if (lotBean.getSCRAPS().size() == 0) {
            requestScrap(lotBean.getLOTID());
        } else {
            scrapListAdapter.setNewData(lotBean.getSCRAPS());
        }
    }

    private void requestScrap(String lotId) {
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotId);
            jsonObject1.put("ACTIONTYPE", "GETLOTINFO");
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<ResponseQueryBean>>(
                new TypeToken<ResponseBean<ResponseQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ResponseQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    scrapListAdapter.setNewData(response.body().getBody().getSCRAPCODES());
                }
            }

            @Override
            public void onError(Response<ResponseBean<ResponseQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "工单模式报废查询");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }
}
